//
//  MainScrollV.m
//  ZITHER
//
//  Created by weixin on 2018/8/27.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainScrollV.h"
#import "CustomCameraVC.h"
#import "MainAntistopVC_V4.h"
#import "MainDefaulSearchVC_V4.h"
#import "MyEnquiryListModel.h"
#import "GoodsModel.h"
#import "MyEnquiryEndListNewModel.h"
#import "MyEnquiryListNewModel.h"
#import "MainDiscernVC.h"
#import "Seach_FittrateSeting_VC_WX.h"


#define bottomH (Is_Iphone_X_Series ? 80 : 45)


@implementation MainScrollV

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.pagingEnabled = YES;
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        [self addView];
        [self addSubview:self.tbv3];
        
        self.backgroundColor = HexColor(0xf5f5f5);
        
    }
    return self;
}


-(void)addView{
    
    //    NSLog(@"self.frame.size.height = = %f",self.frame.size.height);
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-kStatusBarHeight-100-50-kBottomSafeHeight+70)];
    view.backgroundColor = HexColor(0xf5f5f5);
    [self addSubview:view];
    
    for (int i = 0; i<4; i++) {
        
        CGFloat imgH = kScreenW*115/375.0;
        UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0,10+i*(imgH+15),kScreenW,imgH)];
        imageV.backgroundColor = HexColor(0xf5f5f5);
        imageV.contentMode = 2;
        imageV.userInteractionEnabled = YES;
        [self addSubview: imageV];

        if (i == 0) {
            imageV.image = [UIImage imageNamed:@"Home_Search_Image_searchByImage"];

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [[tap rac_gestureSignal] subscribeNext:^(id x) {
                CustomCameraVC *vc = [[CustomCameraVC alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.getNav pushViewController:vc animated:YES];
            }];
            [imageV addGestureRecognizer:tap];
            
        }else if (i ==1 ){
            imageV.image = [UIImage imageNamed:@"Home_Search_Image_searchByKeyword"];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [[tap rac_gestureSignal] subscribeNext:^(id x) {
                
                
                MainAntistopVC_V4 *vc = [[MainAntistopVC_V4 alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.getNav pushViewController:vc animated:YES];
            }];
            [imageV addGestureRecognizer:tap];
        }else if (i ==2 ){
            imageV.image = [UIImage imageNamed:@"Home_Search_Image_searchByTag"];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [[tap rac_gestureSignal] subscribeNext:^(id x) {
                [Tools showTips:@"暂未开放品牌榜"];
            }];
            [imageV addGestureRecognizer:tap];
        } else{
            imageV.image = [UIImage imageNamed:@"Home_Search_Image_searchMatch"];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [[tap rac_gestureSignal] subscribeNext:^(id x) {
                Seach_FittrateSeting_VC_WX *vc = [[Seach_FittrateSeting_VC_WX alloc]init];
                [self.getNav pushViewController:vc animated:YES];
            }];
            [imageV addGestureRecognizer:tap];
        }
        
    }
    
}




-(NSMutableArray *)setdic:(NSString*)index
{
    NSMutableArray *arrM =self.dicM[index];
    if (arrM ==nil) {
        arrM = [[NSMutableArray alloc]init];
        [self.dicM setObject:arrM forKey:index];
    }
    return arrM;
}

#pragma mark --  scrollView代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    for (int i =0; i<self.arrTitle.count; i++) {
        if (scrollView.contentOffset.x==SCREEN_WIDTH*i) {
            CGFloat index = scrollView.contentOffset.x/SCREEN_WIDTH;
            NSLog(@"scrollView.contentOffset.x = %f",index);
            NSString *str = [NSString stringWithFormat:@"%f",index];
            NSInteger i = [str integerValue];
            _block(i);
        }
    }
}

- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

- (BOOL)isPureNumandCharacters:(NSString *)string
{
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(string.length > 0)
    {
        return NO;
    }
    return YES;
}

-(void)setDicM:(NSMutableDictionary *)dicM
{
    _dicM = dicM;
}

-(void)setIndex:(NSInteger)index
{
    _index = index;
}

-(void)setArrTitle:(NSMutableArray *)arrTitle{
    _arrTitle = arrTitle;
    self.contentSize = CGSizeMake(kScreenW*arrTitle.count,0);
//    @"Home_SegCtrl_search_active",
//    @"Home_SegCtrl_demand_active",
//    @"Home_SegCtrl_tracking_active",
//    @"Home_SegCtrl_hot_active"]];
    
    
}



- (MainScrollTbv *)tbv3{
    if (!_tbv3) {
        _tbv3 = [[MainScrollTbv alloc]initWithFrame:CGRectMake(kScreenW, 0 , kScreenW, kScreenH-kStatusBarHeight-100-50-kBottomSafeHeight+70)];
        _tbv3.index = 2;
        __weak __typeof(self)weakSelf = self;
        _tbv3.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.tbv3.page++;
            [weakSelf reloadDataTB:2 location:YES];
        }];
        _tbv3.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.tbv3.page = 0;
            [weakSelf reloadDataTB:2 location:NO];
        }];
        [_tbv3.mj_header beginRefreshing];
    }
    return _tbv3;
}


-(void)loaddata{
    //cell 给每个数据传入 arrM
    //  cell.TB.arrM = arrM; // 给 TBV 填充数据源
    
    /*
     cell.indexBox = indexPath.row; //传入对应第几个 标识   --判断当前产品数据是否为空  是的话就加载(砍掉)改成第一次进来就加载数据 这个可以砍掉了不需要
     cell.arrTitle = self.arrTitle; // 传入一共几个 标题
     [cell setBlcok:^(NSInteger index, NSMutableArray *arrM) {
     [self.dicM setValue:arrM forKey:[NSString stringWithFormat:@"%ld",index]];
     //        [self reloadData];
     }];
     */
}
-(void)loadgetMyEndEnquiryList:(bool)location{
    //已结束询价
//    [[serviceDefualt loadgetMyEndEnquiryList:self.tbv1.page reponseObjctClass:[MyEnquiryListModel class]]subscribeNext:^(ResponseData *  x) {
//        if (x.success) {
//
//            MyEnquiryEndListNewModel *model = [MyEnquiryEndListNewModel yy_modelWithJSON:x.responseRawData];
//
//            if (model.data.count==0) {
//                if (!location) {
//                    if (kUSER_INFO_MANAGER.getMyEnquiryList ==NO) {
//                        kUSER_INFO_MANAGER.getMyEnquiryList = NO;
//                    }
//                }
//
//            } else {
//
//                if (!location) {
//                    if (kUSER_INFO_MANAGER.getMyEnquiryList != YES) {
//                        kUSER_INFO_MANAGER.getMyEnquiryList = YES;
//                    }
//                    self.arrMSource[1] = [[NSMutableArray alloc]init];
//                    self.tbv1.arrMSource [1] =  [[NSMutableArray alloc]init];
//                }
//                for (MyEnquiryListDataNewModel *modelData in model.data) {
//                    [self.arrMSource[1] addObject:modelData];
//                    [self.tbv1.arrMSource[1] addObject:modelData];
//                }
//            }
//            [self.tbv1 reloadData];
//
//        }else{
//            if (kUSER_INFO_MANAGER.userIsLogin && [x.errorInfo isEqualToString:@"未登录"]) {
//                 
//            }else{
//
//            }
//
//        }
//        [self endRefreshing:self.tbv1];
//
//    }];
}

-(void)reloadDataTB:(NSInteger)index location:(bool)location
{

        //获取热门个数据
//        [[serviceDefualt loadGoodsHotList:self.tbv3.page reponseObjctClass:[GoodsModel class]]subscribeNext:^(ResponseData *x) {
//            if (x.success) {
//                NSArray *arr = x.responseRawData[@"data"][@"goodVOs"];
//                
//                if (!location) {
//                    self.arrMSource[3] = [[NSMutableArray alloc]init];
//                    self.tbv3.arrMSource[3] = [[NSMutableArray alloc]init];
//                }else{
//                    if (arr.count==0) {
//                        [self.tbv3.mj_footer endRefreshingWithNoMoreData];
//                        return ;
//                    }
//                }
//                for (NSDictionary *dic in  arr) {
//                    goodVOsModel *model = [goodVOsModel yy_modelWithJSON:dic];
//                    [self.arrMSource[3] addObject:model];
//                    [self.tbv3.arrMSource[3] addObject:model];
//                }
//                [self.tbv3 reloadData];
//            }else{
//                 [Tools showTips:x.errorInfo];
//            }
//            [self endRefreshing:self.tbv3];
//        }];
    
}


-(void)endRefreshing:(UITableView*)tbv{
    [tbv.mj_header endRefreshing];
    [tbv.mj_footer endRefreshing];
    
}

-(NSMutableArray *)arrMSource{
    if (!_arrMSource) {
        _arrMSource = [[NSMutableArray alloc]init];
        for (int i =0; i<4; i++) {
            if (i==0) {
                MyEnquiryListModel *model = [[MyEnquiryListModel alloc]init];
                [_arrMSource addObject:model];
            } else {
                [_arrMSource addObject:[[NSMutableArray alloc]init] ];
            }
        }
    }
    return _arrMSource;
}
@end

